<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Session;

class Fixshenhe extends Base
{
	//维修审核列表
	public function index()
	{
		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] != 0) {

			$fix_details = Db::name('fix_details')->where('flag', 1)->select();
			if (empty($fix_details)) {
				return $this->json_data(0, []);
			}
			//halt($fix_details['pid']);
			$count = count($fix_details);
			foreach ($fix_details as $key => $value) {

				$fix = Db::name('fix')->where('id', $value['pid'])->find();
				$fix_details[$key]['good_name'] = Db::name('good')->where('id', $fix['good_id'])->find()['name'];
				$tagslogs = Db::name('tagslogs')->where('sn', $fix['tagslogs_sn'])->find();
				$fix_details[$key]['tagslogs_sn'] = $tagslogs['sn'];
				$fix_details[$key]['addtime'] = $tagslogs['addtime'] == 0 ? '' : date('Y-m-d', $tagslogs['addtime']);
				$fix_details[$key]['outtime'] = $tagslogs['outtime'] == 0 ? '' : date('Y-m-d', $tagslogs['outtime']);
				$fix_details[$key]['date'] = $value['date'] == 0 ? 0 : date('Y-m-d', $value['date']);
				$fix_details[$key]['fixwhere'] = '用户添加';
				if ($value['where'] == 1) {
					$fix_details[$key]['fixwhere'] = '系统添加';
				}
				// 关联序列号
				$fix_details[$key]['tagslogs_sn1'] = '';
				$more_sn = more_sn($fix['tagslogs_sn']);
				if ($more_sn['status'] == 1) {
					$fix_details[$key]['tagslogs_sn1'] = $more_sn['sn'];
				}
			}
			return $this->json_data($count, $fix_details);
		}
		return $this->fetch();
	}

	//审核1-1
	public function shenhe()
	{
		$data = $this->request->param();
		$info = Db::name('fix_details')->where('id', $data['id'])->find();
		$info['date'] = date('Y-m-d', $info['date']);
		$fix = Db::name('fix')->where('id', $info['pid'])->find();
		$good = Db::name('good')->where('id', $fix['good_id'])->find();
		$this->assign('info', $info);
		$this->assign('fix', $fix);
		$this->assign('good', $good);
		return $this->fetch();
	}

	//审核1-2
	public function shenhe_save()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();
		$post = $data;
		if (empty($post['project'])) {
			$msg = "维修项目不能为空";
			return ['status' => $status, 'msg' => $msg];
		}
		if (empty($post['date'])) {
			$msg = "维修时间不能为空";
			return ['status' => $status, 'msg' => $msg];
		}
		if (empty($post['personnel'])) {
			$msg = "维修人员不能为空";
			return ['status' => $status, 'msg' => $msg];
		}
		//  if(empty($post['price'])){
		// 	 $msg="维修费用不能为空";
		// 	 return ['status'=>$status, 'msg'=> $msg];
		// }
		if (empty($post['mark'])) {
			$msg = "维修说明不能为空";
			return ['status' => $status, 'msg' => $msg];
		}
		unset($data['id']);
		unset($data['send']);
		unset($data['sn']);
		$data['updatetime'] = time();
		$data['flag'] = 0;
		$data['date'] = strtotime($data['date']);
		$data['check_id'] = Session::get('user_id');
		$data['checktime'] = time();
		$res = Db::name('fix_details')->where('id', $post['id'])->update($data);
		if ($res) {
			// 判断是否发短信
			if ($post['send'] == "1") {
				// 发送审核短信到用户
				$msg_temp = get_msg_temp(5);
				$this->logger($msg_temp['is_on']);
				$this->logger($post['sn']);
				$this->logger($data['telphone']);
				if ($msg_temp['is_on'] == 1) {
					if ($msg_temp['type'] == 1 || $msg_temp['type'] == 2) { //通知短信
						send_mobile_message($data['telphone'], "shpass", $post['sn']);
					}
					if ($msg_temp['type'] == 1 || $msg_temp['type'] == 3) { //通知邮箱
						if (!empty($data['email'])) {
							//sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
							sendToMail($data['email'], $msg_temp['email_title'], $msg_temp['email_content']);
						}
					}
				}
				//send_mobile_message($data['telphone'], "shpass");
			}
			adminlog('修改维修记录', '修改');
			$status = 1;
			$msg = "修改成功";
		}
		return ['status' => $status, 'msg' => $msg];
	}

	public function logger($content)
	{ //存储日志
		$logSize = 100000; //定义日志大小
		$log = "log.txt"; //定义日志名字
		if (file_exists($log) && filesize($log) > $logSize) { //如果日志存在并且大于定义的大小
			unlink($log); //删除
		}
		//文件写入1.名字2.内容，+时间防止重复3.内容往后加而不是覆盖
		file_put_contents($log, date('H:i:s') . " " . $content . "\n", FILE_APPEND);
	}
}
